home *** CD-ROM | disk | FTP | other *** search
/ CD BIT 75 / CD BIT 75.iso / Software / hbqsd-download.exe / $INSTDIR / history.js < prev    next >
Encoding:
Text File  |  2004-10-30  |  5.4 KB  |  247 lines

  1. // fancy UI: history and cut and paste (via arrow and control keys)
  2.  
  3. histarray = [];
  4. histedit = new Array(historylength + 1);
  5. histcurr = 0;
  6.  
  7. restoreHistory();
  8.  
  9.  
  10. //clear the history
  11. function clearHistory()
  12. {
  13.   histarray = [];
  14.   saveHistory();
  15.   histcurr = 0;
  16. }
  17.  
  18.  
  19. // save the history array
  20. function saveHistory()
  21. {
  22.   try
  23.   {
  24.     writeFile("history.txt", histarray.join('\r\n'));
  25.   }
  26.   catch(e) { }
  27. }
  28.  
  29. // restore at most historylength entries from the saved history
  30. function restoreHistory()
  31. {
  32.   var historyFileContent = null;
  33.   try
  34.   {
  35.     historyFileContent = readFile("history.txt");
  36.   }
  37.   catch (e) {}
  38.   
  39.   var loaded = null;
  40.   if (historyFileContent)
  41.     loaded = historyFileContent.replace(/\r\n/g, '\n').split('\n');
  42.   else // history.txt probably doesn't exist
  43.     loaded = new Array(0);
  44.  
  45.   for (var i = loaded.length - 1; i >= 0; i--)
  46.   {
  47.     if (loaded[i] && loaded[i] != "")
  48.       histarray.push(loaded[i]);
  49.     if (histarray.length >= historylength)
  50.       break;
  51.   }
  52.   histarray.reverse();
  53.   histcurr = histarray.length;
  54.   histedit[histarray.length] = "";
  55. }
  56.  
  57. // add an item to the history
  58. function addhist(t)
  59. {
  60.   // See if it's already in the history so we can remove it
  61.   var i;
  62.   for (i = 0; i < histarray.length; i++)
  63.     if (histarray[i] == t) break;
  64.  
  65.   // Not there, but history is maxed out: remove the 0th.
  66.   if (i == histarray.length && histarray.length == historylength)
  67.     i = 0;
  68.     
  69.   // Shift down all the entries after the one to be removed
  70.   for (; i < histarray.length - 1; i++)
  71.     histarray[i] = histarray[i + 1];
  72.  
  73.   // Store the new item in the history
  74.   histarray[i] = t;
  75.     
  76.   // Save it baby
  77.   saveHistory();
  78.  
  79.   // History edits get cleared whenever something new goes in
  80.   clearhistedits();
  81.   histcurr = i + 1;
  82. }
  83.  
  84. // get the current history entry
  85. function currhistedit()
  86. {
  87.   if (histedit[histcurr] != null)
  88.     return histedit[histcurr];
  89.   if (histcurr >= histarray.length)
  90.     return (histcurr > 0 ? histarray[histarray.length - 1] : "");
  91.   return histarray[histcurr];
  92. }
  93.  
  94. // clear the history edits
  95. function clearhistedits()
  96. {
  97.   for (var i = 0; i < histedit.length; i++)
  98.   {
  99.     histedit[i] = null;
  100.   }
  101. }
  102.  
  103. // advance the currently viewed history entry +1 or -1
  104. function histeditmove(t, i)
  105. {
  106.   if (t != currhistedit())
  107.     histedit[histcurr] = t;
  108.  
  109.   // special case: first ctrl-p and no edits were done
  110.   if (i == -1 && histcurr == histarray.length && histedit[histcurr] == null)
  111.   {
  112.     histedit[histcurr] = "";
  113.     if (histcurr > 0) histcurr--;
  114.   }
  115.     
  116.   // no movement
  117.   if (i == 0) return;
  118.  
  119.   // filter when somebody has typed !prefix then ctrl-p
  120.   var scan = histcurr + i;
  121.   if (scan > histarray.length || scan < 0) return;
  122.  
  123.   var filter = histedit[histarray.length];
  124.   if (filter != null && filter.match(/^!\S/) && filter != "!!")
  125.     filter = "\\b" + filter.substring(1);
  126.   else if (filter != null && filter.match(/\S!$/) && filter != "!!")
  127.     filter = "\\b" + filter.substring(0,filter.length - 1);
  128.   else filter = "";
  129.  
  130.   while (scan < histarray.length && !histarray[scan].match(filter))
  131.   {
  132.     scan += i;
  133.     if (scan < 0) return;
  134.   }
  135.  
  136.   histcurr = scan;
  137. }
  138.  
  139. function histsearch( t, shift )
  140. {
  141.   if ( searchPrefix == '' )
  142.     searchPrefix = t;
  143.  
  144.   // Escape every letter in the searchPrefix, because it might contain chars with special meaning in a regexp
  145.   var escapedString = escapeString( searchPrefix );
  146.  
  147.   var re;
  148.   try
  149.   {
  150.     re = new RegExp( escapedString, "i" );
  151.   }
  152.   catch(e)
  153.   {
  154.     alert("An error (" + e.description + ") occurred during the history search");
  155.     return;
  156.   }
  157.   
  158.   if (!shift) // Search back through history
  159.   {
  160.     for ( var i = histcurr - 1; i >= 0; i-- )
  161.     {
  162.       if ( histarray[i].match( re ) )
  163.       {
  164.         histcurr = i;
  165.         setSearchWindowText(currhistedit());
  166.         return;
  167.       }
  168.     }
  169.  
  170.     for ( var i = histarray.length - 1; i >= histcurr; i-- )
  171.     {
  172.       if ( histarray[i].match( re ) )
  173.       {
  174.         histcurr = i;
  175.         setSearchWindowText(currhistedit());
  176.         return;
  177.       }
  178.     }
  179.   }
  180.   else // Search forward through history
  181.   {
  182.     for ( var i = histcurr + 1; i < histarray.length; i++ )
  183.     {
  184.       if ( histarray[i].match( re ) )
  185.       {
  186.         histcurr = i;
  187.         setSearchWindowText(currhistedit());
  188.         return;
  189.       }
  190.     }
  191.  
  192.     for ( var i = 0; i <= histcurr; i++ )
  193.     {
  194.       if ( histarray[i].match( re ) )
  195.       {
  196.         histcurr = i;
  197.         setSearchWindowText(currhistedit());
  198.         return;
  199.       }
  200.     }
  201.   }
  202. }
  203.  
  204.  
  205. // recent changes a !foo textbox into whatever it means
  206. // it returns false if there is no match
  207.  
  208. function recent()
  209. {
  210.   var t = escapeString( document.deff.q.value );
  211.  
  212.   if (!t.match(/^!\S/)) return true;
  213.  
  214.   if (t == "!!")
  215.   {
  216.     histcurr = histarray.length;
  217.     histeditmove(t, -1);
  218.     if (histcurr == histarray.length) return false;
  219.   }
  220.   else
  221.   {
  222.     // this bit of code searches backwards for !foo
  223.  
  224.     // set the filter then behave like a ctrl-p
  225.     histcurr = histarray.length;
  226.     histeditmove(t, -1);
  227.  
  228.     // return true if no match
  229.     if (histcurr == histarray.length) return false;
  230.   }
  231.  
  232.   setSearchWindowText(currhistedit());
  233.   return true;
  234. }
  235.  
  236. function escapeString( s )
  237. {
  238.   if ( !s.length )
  239.     return s;
  240.   var es = '';
  241.   for (var i = 0; i < s.length; i++)
  242.   {
  243.     es += "\\x" + s.charCodeAt(i).toString(16);
  244.   }
  245.   return es;
  246. }
  247.